$(function () {
    /*初始化*/
    var liHeight = $('.filter_list li').height();
    for (var i = 0; i < $('.filter_list li').length; i++) {
        var listHeight = $('.filter_list li').eq(i).find('.list')[0].offsetHeight;
        if (listHeight - 5 > liHeight) {
            $('.filter_list li').eq(i).find('.morebtn').show();
        }
    }
    /*加入订单 飞入效果*/
    var cartOffset = $(".fixed_cart").offset();
    function flyToCart(ele, callback) {
        var $addcar = ele;
        var img = ele.find('img').attr('src');
        var flyer = $('<img class="u-flyer" src="' + img + '">');
        flyer.fly({
            start: {
                left: ele.find('.addbtn').offset().left-$('body').scrollLeft(),
                top: ele.find('.addbtn').offset().top-$('body').scrollTop()
            },
            end: {
                left: cartOffset.left + 10,
                top: cartOffset.top + 10,
                width: 0,
                height: 0
            },
            onEnd: function () {
                callback();
                this.destory();
            }
        });
    }

    /*加入订单弹框*/
    $(".waterfall_box").on('click', '.addbtn', function (event) {
        var $this = $(this);
        bombBox({
            entrance: $('.add_msgbox'),
            initFun: function () {
            },
            confirm: function () {
                flyToCart($this.parents('.waterfall_item'), function () {
                    console.log("加入购物车");
                    
                    //todo  底部购物车状态  ajax
                })
            }
        })
    })


    /*更多展开*/
    $(".filter_list").on('click', '.morebtn', function () {
        var $this = $(this);
        if ($this.hasClass('up')) {
            $this.parents('li').css('height', liHeight);
            $this.removeClass('up');
        } else {
            $this.parents('li').css('height', $this.parents('li').find('.list')[0].offsetHeight);
            $this.addClass('up');
        }
    })

    /*选中筛选条件*/
    $(".filter_list").on('click', '.list span', function () {
        var $this = $(this);
        var text = $this.html(),
            dataid = $this.attr('data-id')
        if ($this.hasClass('active')) {
            $this.removeClass('active');
            $('.crumbs_nav .sellist').find('[data-id=' + dataid + ']').remove();
            /*todo ajax*/
        } else {
            $this.addClass('active');
            $('.crumbs_nav .sellist').append('<span data-id=' + dataid + '>' + text + '</span>')
            /*todo ajax*/
        }
    })

    /*计数器调用*/
    $(".counter_box").count({
        limitnum: 100,
        addAction: function (num) {
            console.log('增加回调');
        },
        minusAction: function (num) {
            console.log("减少回调")
        }
    })
    /*瀑布流*/
    function waterFall() {
        msnry = new Masonry('.waterfall_box', {
            // options
            itemSelector: '.waterfall_item',
            percentPosition: true,
            columnWidth: '.waterfall_item'
        });
        imagesLoaded(document.querySelector('.waterfall_box'), function () {
            msnry.layout();
        });
    }
    waterFall();
    var totalPages = 7;
    var loader = new Loadmore($('.waterfall_box')[0], {
        loadMore: function (page, done) {
            setTimeout(function () {
                var html = '<div class="waterfall_item"><div class="imgbox"><img src="images/w02.jpg" /><div class="btnbox"><button class="dbtn_blue addbtn">加入订单</button></div></div></div>'
                $('.waterfall_box').append(html);
                waterFall();
                
                if (page == totalPages) {
                    loader.destroy();
                }

                done();

            }, 500);

        },
        bottomBuffer: 0 //预加载临界值
    });

})


